package com.spt.springbootzhsx.controller;

import com.spt.springbootzhsx.entity.User;
import com.spt.springbootzhsx.entity.back.UserBack;
import com.spt.springbootzhsx.entity.count.UserWatchRecord;
import com.spt.springbootzhsx.mapper.UserMapper;
import com.spt.springbootzhsx.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;

@RestController
@RequestMapping(value="/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping(value="/regist")
    public Integer registUser(@RequestBody User user){
        return userService.registUser(user);
    }

    @GetMapping(value="/getUserByUid/{uid}")
    public User getUserByUid(@PathVariable("uid") Integer uid){
        return userService.getUserByUid(uid);
    }

    @GetMapping(value="/vipPay/{uid}")
    public User vipPay(@PathVariable("uid") Integer uid){
        return userService.vipPay(uid);
    }

    @GetMapping(value="/findPassword_step1/{uid}")
    public String findPassword_step1(@PathVariable("uid") Integer uid){
        return userService.findPassword_step1(uid);
    }

    @GetMapping(value="/findPassword_step2/{uid}/{password}/{yzm}")
    public String findPassword_step2(@PathVariable("uid") Integer uid,@PathVariable("password") String password,@PathVariable("yzm") Integer yzm){
        return userService.findPassword_step2(uid,password,yzm);
    }

    @PostMapping(value="/save/{uid}")
    public String save(@RequestBody MultipartFile file,@PathVariable("uid") Integer uid){
        try{
            System.out.println("**********正在上传*********");

            //文件名
            String filename = uid.toString()+".jpg";
            //文件大小
            Long size = file.getSize();
            //文件类型
            String type = file.getContentType();

            String filePath = "D://Web课设前端//web-vue2//src//assets//" + filename;
            System.out.println("文件存储路径为：" + filePath);

            File demoFile = new File(filePath);
            file.transferTo(demoFile);

            userMapper.updateHeadSrc(uid,filename);
            String src = "assets/"+filename;

            System.out.println("上传成功！");
            return  "OK";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return  "NO";
        }
    }


   @GetMapping(value="/getUserRecord/{uid}")
    public ArrayList<UserWatchRecord> getUserRecord(@PathVariable("uid") Integer uid){
        return userService.getRecordByUid(uid);
   }

   @GetMapping(value="/deleteUserAndVip/{uid}")
    public String deleteUserAndVip(@PathVariable("uid") Integer uid){
        return userService.deleteUserAndVip(uid);
   }

   @GetMapping(value="/getUserAndVip")
    public ArrayList<UserBack> getUserAndVipByUid(){
        return userService.getUserAndVip();
   }

    @PostMapping(value="/updateUser")
    public User updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping(value="/saveHeadSrc/{uid}")
    public String saveHeadSrc(@RequestBody MultipartFile file,@PathVariable("uid") Integer uid){
        try{
            System.out.println("**********正在上传*********");

            //文件名
            String filename = uid.toString()+".jpg";
            //文件大小
            Long size = file.getSize();
            //文件类型
            String type = file.getContentType();

            String filePath = "D://Web课设前端//web-vue2//src//assets//" + filename;
            System.out.println("文件存储路径为：" + filePath);

            File demoFile = new File(filePath);
            file.transferTo(demoFile);

            User user=userService.getUserByUid(uid);
            String src = "assets/"+filename;
            userMapper.updateHeadSrc(uid,uid.toString()+".jpg");


            System.out.println("上传成功！");
            return  "OK";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return  "NO";
        }
    }

    @GetMapping(value = "/getOpenid/{token}")
    public String getOpenId(@PathVariable("token") String token){
        return userService.getOpenIdByToken(token);
    }

    @GetMapping(value="/getSessionKey/{token}")
    public String getSessionKey(@PathVariable("token") String token){
        return userService.getSessionKeyByToken(token);
    }

    @GetMapping(value="/getUnionId/{token}")
    public String getUnionId(@PathVariable("token") String token){
        return userService.getUnionIdByToken(token);
    }
}
